const app = getApp()
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
function initSubMenuDisplay() {
  return ['hidden', 'hidden', 'hidden'];
}

// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //获取经纬度
    page: '{"page_size":"60","page_no":"1"}',
    subMenuDisplay: initSubMenuDisplay(),
    motto: '示例小程序-获取当前地理位、速度',
    userInfo: {},
    defaultimg:'http://oss-img.meitianhui.com/test/communityDocs/201801/51a75f1fc3f445cba238b145faff1d41.jpg',
    hasLocation: false,
    location: {},
    list: {},
    dianpu: {
      items: [],
      page: {
        page_no: 0,
        page_size: 10,
      },
      paginate: {}
    },
    prompt: {
      hidden: 0,

      items: []
    },
    menu: [
      ['<500m', '<1km', '<3km', '全城'],
      ['全部', '便利店', '美食', '水果鲜花', '购物', '生活服务', '医药', '美业', '休闲娱乐', '住宿', '更多'],
      ['距离优先']
    ],


  },

  //获取定位测试
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    this.initMenu();
    this.initHighlight();
    this.memberDemo();
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  },





  initData() {
    this.setData({
      goldcoin: {
        items: [],
        params: {
          page: 0,
          limit: 10,
        },
        paginate: {}
      }
    })
  },
  //点击事件获门店ID请进行页面跳转

  listClick: function (event) {

    var p = event.currentTarget.id
    wx.navigateTo({ url: '/pages/detail/detail?stores_id=' + p })

  },

  memberDemo: function (e) {
    var that = this;
    let pageData = {
      page_no: this.data.dianpu.page.page_no + 1,
      page_size: this.data.dianpu.page.page_size,
    }
    const data = {
      //测试环境默认设置的
      page: JSON.stringify(pageData),
      service: 'stores.consumer.nearbyStoresTypeListPageFind',
      params: '{"range":"3000000","latitude":"' + app.globalData.locationInfo.latitude + '","longitude":"' + app.globalData.locationInfo.longitude + '"}',
      url_type: 'member',

      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code


    }

    const dianpu = this.data.dianpu
    const prompt = this.data.prompt
    const params = dianpu.params
    api.requestData
      ({
        data,
        success: (res) => {
          var resData = res.data;
          console.log(resData)
          //dianpu.items = resData.data.list;
          console.log(this.data.dianpu)
          var docList = res.data.data.doc_url
          var doc_url_key = Object.keys(res.data.data.doc_url)
          var itemList = resData.data.list
          console.log("itemListitemListitemListiteemListitemListitemListitemListitemListitemList")
          console.log(itemList)
          for (var i = 0; i < itemList.length; i++) {

            //console.log(itemList[i])
            //logo_pic_path

            var item_pic_info_str = itemList[i].neighbor_pic_path;

            //console.log(item_pic_info_str)

            for (var e = 0; e < doc_url_key.length; e++) {
              if (item_pic_info_str === doc_url_key[e]) {
                itemList[i].pic_info = docList[doc_url_key[e]]
                itemList[i].pic_info = itemList[i].pic_info.replace("https", "http")
                console.log(itemList[i].pic_info)
              } else {
                itemList[i].pic_info == ''
              }

            }
          }



          dianpu.items = [...dianpu.items, ...resData.data.list]
          dianpu.page.page_no = res.data.data.page.page_no
          dianpu.page.page_size = res.data.data.page.page_size
          //paginate.total_page = res.data.data.page.total_page
          dianpu.paginate.total_count = res.data.data.page.total_count
          //判断hasNext
          if (dianpu.paginate.total_count > dianpu.page.page_no * dianpu.page.page_size) {
            console.log("后面有数据 还没有查询完")
            dianpu.paginate.hasNext = true
          } else {
            console.log("后面没有数据了，不要再拉了")
            dianpu.paginate.hasNext = false
          }

          this.setData({
            dianpu: dianpu,

          })

        },
        fail: (res) => {
          console.log("fail 返回 res.data>>>" + res.data);
        },
        complete: (res) => {
          console.log("complete 返回 res.data>>>" + res.data);
        }
      })


  },




  //筛选
  selectItem: function (type_id, range) {


    console.log(type_id)
    console.log(range)

    var that = this;
    const data = {
      //测试环境默认设置的
      page: '{"page_size":"10","page_no":"1"}',
      service: 'stores.consumer.nearbyStoresTypeListPageFind',
      params: '{"range":"' + range + '","latitude":"' + app.globalData.locationInfo.latitude + '","stores_type_group":"' + type_id + '","longitude":"' + app.globalData.locationInfo.longitude + '"}',
      url_type: 'member',

      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code
    }

    const dianpu = this.data.dianpu
    api.reqData
      ({
        data,
        success: (res) => {

          var resData = res.data;
          var resData = res.data;

          dianpu.items = resData.data.list;
          //dianpu.items = [...dianpu.items, ...resData.data.list]
          console.log("sxscscscscscscscscsc>>>" + dianpu);
          var docList = res.data.data.doc_url
          var doc_url_key = Object.keys(res.data.data.doc_url)

          console.log(doc_url_key)
          var itemList = this.data.dianpu.items
          for (var i = 0; i < itemList.length; i++) {

            //console.log(itemList[i])
            //logo_pic_path

            var item_pic_info_str = itemList[i].neighbor_pic_path;
            console.log("****")
            console.log(item_pic_info_str)
            if (item_pic_info_str != '') {
              for (var e = 0; e < doc_url_key.length; e++) {
                if (item_pic_info_str === doc_url_key[e]) {
                  itemList[i].pic_info = docList[doc_url_key[e]]
                  itemList[i].pic_info = itemList[i].pic_info.replace("https", "http")
                }
              }
            } else {
              itemList[i].pic_info = ''
            }
          }

          dianpu.items = [...dianpu.items, ...resData.data.list]
          dianpu.page.page_no = res.data.data.page.page_no
          dianpu.page.page_size = res.data.data.page.page_size
          //paginate.total_page = res.data.data.page.total_page
          dianpu.paginate.total_count = res.data.data.page.total_count
          //判断hasNext
          if (dianpu.paginate.total_count > dianpu.page.page_no * dianpu.page.page_size) {
            console.log("后面有数据 还没有查询完")
            dianpu.paginate.hasNext = true
          } else {
            console.log("后面没有数据了，不要再拉了")
            dianpu.paginate.hasNext = false
          }
          this.setData({
            dianpu: dianpu
          })

        },
        fail: (res) => {
          console.log("fail 返回 res.data>>>" + res.data);
        },
        complete: (res) => {
          console.log("complete 返回 res.data>>>" + res.data);
        }
      })
  },




  //下拉菜单
  list: function (e) {
    var index = e.currentTarget.dataset.nav;
    var open = this.data.open;
    var isMasked = this.data.isMasked;
    var show = this.data.show;
    var naviIndex = this.data.naviIndex;
    for (var i = 0; i < this.data.menu.length; i++) {
      // 除当前元素外，其他元素一律取false
      if (i != index) {
        open[i] = false;
      }
      show[i] = false;
    }
    if (open[index] == true) {
      // 当前元素取反
      open[index] = false;
      isMasked = false;
      naviIndex = -1;
    } else {
      open[index] = true;
      isMasked = true;
      naviIndex = index
    }
    show[index] = true;
    this.setData({
      open: open,
      isMasked: isMasked,
      show: show,
      naviIndex: naviIndex
    });
  },
  hideBackground: function (e) {
    this.initMenu();
  },
  initMenu: function () {
    var open = this.data.menu.map(() => {
      return false;
    });
    var show = this.data.menu.map(() => {
      return false;
    });
    this.setData({
      open: open,
      show: show,
      isMasked: false,
      naviIndex: -1
    })
  },
  initHighlight: function () {
    // 二级菜单选中高亮
    var highlight = this.data.menu.map((item) => {
      return item.map(sub => {
        return '';
      });
    });
    this.setData({
      highlight: highlight
    });
  },
  subMenuTapped: function (e) {
    // 子菜单点击
    var that = this
    var naviIndex = e.currentTarget.dataset.naviIndex;
    var rowIndex = e.currentTarget.dataset.rowIndex;
    console.log(naviIndex)
    console.log(rowIndex)
    if (naviIndex == '1' && rowIndex == '1') {
      var type_id = "01"
      var range = ""
      this.selectItem(type_id, range)
    } else if (naviIndex == '1' && rowIndex == '2') {
      var type_id = "02"
      var range = ""
      this.selectItem(type_id, range)
    } else if (naviIndex == '1' && rowIndex == '3') {
      var type_id = "03"
      var range = ""
      this.selectItem(type_id, range)
    } else if (naviIndex == '1' && rowIndex == '4') {
      var type_id = "04"
      var range = ""
      this.selectItem(type_id, range)
    } else if (naviIndex == '1' && rowIndex == '5') {
      var type_id = "05"
      var range = ""
      this.selectItem(type_id, range)
    } else if (naviIndex == '1' && rowIndex == '6') {
      var type_id = "06"
      var range = ""
      this.selectItem(type_id, range)
    } else if (naviIndex == '1' && rowIndex == '0') {
      var type_id = ""
      var range = ""
      this.selectItem(type_id, range)
    }
    else if (naviIndex == '1' && rowIndex == '7') {
      var type_id = "07"
      var range = ""
      this.selectItem(type_id, range)
    }
    else if (naviIndex == '1' && rowIndex == '8') {
      var type_id = "08"
      var range = ""
      this.selectItem(type_id, range)
    }
    else if (naviIndex == '1' && rowIndex == '9') {
      var type_id = "09"
      var range = ""
      this.selectItem(type_id, range)
    }
    else if (naviIndex == '1' && rowIndex == '10') {
      var type_id = "10"
      var range = ""
      this.selectItem(type_id, range)
    }
    else if (naviIndex == '0' && rowIndex == '0') {
      var type_id = ""
      var range = "500"
      this.selectItem(type_id, range)
    }
    else if (naviIndex == '0' && rowIndex == '1') {
      var type_id = ""
      var range = "1000"
      this.selectItem(type_id, range)
    }
    else if (naviIndex == '0' && rowIndex == '2') {
      var type_id = ""
      var range = "3000"
      this.selectItem(type_id, range)
    } else if (naviIndex == '0' && rowIndex == '3') {
      var type_id = ""
      var range = ""
      this.selectItem(type_id, range)
    }
    //wx.showToast({
    //title: '点中了 ' + naviIndex + '列' + rowIndex + '行'
    //});
    // 获取原高亮状态
    var highlight = this.data.highlight;
    // 消除兄弟亮亮
    highlight[naviIndex] = highlight[naviIndex].map(item => {
      return '';
    });
    // 设定当前高亮状态
    highlight[naviIndex][rowIndex] = 'highlight';
    // 存回高亮数组
    this.setData({
      highlight: highlight,

    });
    console.log(this.data.highlight);
    // 隐藏子菜单
    this.initMenu();

  },

  onTapTag: function (e) {
    this.setData({
      selected: e.currentTarget.dataset.index
    });
  },
  mask1Cancel: function () {
    this.setData({
      mask1Hidden: true
    })
  },
  mask2Cancel: function () {
    this.setData({
      mask2Hidden: true
    })
  },
  onOverallTag: function () {
    this.setData({
      mask1Hidden: false
    })
  },
  onFilter: function () {
    this.setData({
      mask2Hidden: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */




  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //拉到底部刷新
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    if (!this.data.dianpu.paginate.hasNext) return
    this.memberDemo()
  },


})
